package online.hxstart.code.leetcode;

import lombok.var;
import org.junit.jupiter.api.Test;

/**
 * @author xinghu
 * @version Code-fragmentation 1.0
 * @date 2021/8/30 2:09 下午
 */
public class LongestCommonPrefix {

    public String longestCommonPrefix(String[] strs) {
        final int length = strs.length;
        char[][] chars = new char[length][];
        char a = 0;
        int minL = 1;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < minL; i++) {
            boolean b1 = false;
            for (int i1 = 0; i1 < length; i1++) {
                if (i == 0) {
                    final char[] charArray = strs[i].toCharArray();
                    final int i2 = charArray.length;
                    if (i2 == 0) {
                        return "";
                    }
                    chars[i] = charArray;
                    minL = i2;
                }

                if (i1 == 0) {
                    a = chars[i1][i];
                    continue;
                }
                if (a != chars[i1][i]) {
                    b1 = true;
                    break;
                }
            }
            if (b1) {
                break;
            }
            sb.append(a);
        }
        return sb.toString();
    }

    @Test
    public void test() {
        final var prefix = longestCommonPrefix(new String[] {"flower", "flow", "flight"});
        System.out.println(prefix);
    }
}
